<?php
require_once 'Zend/Exception.php';
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Exception.php';
require_once 'Zend/Mail/Transport/Smtp.php';
require_once 'config.php';

try {
	function sendMail($to, $subject, $body, $from = NULL, $bcc = NULL)
	{
		try {
			$mailConfig = array(
				'auth' => 'login',
				'username' => $config['mail']['smtp']['username'],
				'password' => $config['mail']['smtp']['password'],
				'ssl' => 'tls'
			);
			
			$transport = new Zend_Mail_Transport_Smtp($config['mail']['smtp']['host'], $mailConfig);
			
			$mail = new Zend_Mail('UTF-8');
			if(is_null($from)){
				$mail->setFrom('bilgi@feykin.com', 'Feykin Bilgi Servisi');
			}else{
				if (is_array($from))
					$mail->setFrom($from[0], $from[1]);
				else 
					$mail->setFrom($from);
			}
			
			if(is_array($to)){
				$mail->addTo($to[0], $to[1]);
			}else{
				$mail->addTo($to);
			}
			
	    	if(is_array($bcc)){
				$mail->addTo($bcc[0], $bcc[1]);
			}else{
				if(!empty($bcc)) $mail->addBcc($bcc);
			}
			
			$mail->setSubject($subject);
			$mail->setBodyHtml($body);
			
			$mail->send($transport);
			
			return true;
			
	    } catch (Zend_Mail_Exception $e) {
	    	throw new Zend_Exception($e->getMessage());
	    }
	}
	
	$ad		= $_POST['name'];
	$email	= $_POST['mail'];
	$mesaj	= $_POST['message'];
	
	if(filter_var($email, FILTER_VALIDATE_EMAIL)){
		$to			= 'osman@feykin.com';
		$bcc		= Array('bilgi@feykin.com', 'Feykin Bilgi Servisi');
		$from		= Array($email, $ad);
		$subject	= 'Destek - ' . $ad . '('.$email.')';	
		
		if(sendMail($to, $subject, $mesaj, $from, $bcc)){
			echo "<div id='ok'><p>Thanks for support message...</p></div> ";
		}
		else
		{
			echo "<div id='error'><P>Uppss? Sorry... We receive message, plase try again! </p> </div>";
		}
	}else{
		echo "<div id='error'><P> Sorry! Your e-mail adress is not valid! :( </p></div>";
	}
} catch (Exception $e) {
	
}